/*
 * @Author: zi.yang
 * @Date: 2022-12-07 19:13:12
 * @LastEditors: zi.yang
 * @LastEditTime: 2022-12-07 20:43:25
 * @Description: chrome 清除全局滤镜
 * @FilePath: /chrome-extensions-clean-filter/index.js
 */
let idx = 0

const getFilterStyle = (dom) => {
  if (dom.style.filter && dom.style.filter !== 'none') return true
  let style = getComputedStyle(dom)
  return style.filter && style.filter !== 'none'
}

const findFilterTag = (dom = document.body) => {
  for (let index = 0; index < dom.children.length; index++) {
    const element = dom.children[index]
    if (getFilterStyle(element)) {
      idx = 2
      element.style.filter = 'none'
      break;
    } else {
      findFilterTag(element)
    }
  }
  idx += 1;
}

const timer = setTimeout(() => {
  clearTimeout(timer)
  // body
  const body = document.body
  if (getFilterStyle(body)) {
    body.style.filter = 'none'
    return
  }
  // html
  const html = document.querySelector('html')
  if (getFilterStyle(html)) {
    html.style.filter = 'none'
    return
  }

  while (idx <= 2) {
    findFilterTag()
  }
}, 1000);
